package org.acghub.config;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/25/星期五 12:17
 * @Version 1.0
 */
public interface TableDataImporter {

    ImportData importData(File file) throws TableDataImportException;


    @Getter
    @Slf4j
    class ImportData implements Iterable<TableDataRow>, DataVerifiable {

        private final String fullClassName;

        private final List<TableDataRow> rows = new ArrayList<>();

        private Class<? extends AbstractConf> confClazz;

        private boolean verify = false;

        public ImportData(String fullClassName) {
            this.fullClassName = fullClassName;
        }

        public void add(TableDataRow row) {
            rows.add(row);
        }

        @Override
        public Iterator<TableDataRow> iterator() {
            return rows.iterator();
        }

        @Override
        public void verify() throws DataVerifyException {
            if (verify) {
                return;
            }
            try {
                confClazz = (Class<? extends AbstractConf>) Class.forName(fullClassName);
            } catch (Exception e) {
                throw new DataVerifyException("fullClassName:" + fullClassName, e);
            }
            verify = true;
        }

        public <C extends AbstractConf> Class<C> getConfClazz() {
            return (Class<C>) confClazz;
        }
    }
}
